<template>
	<view class="content" style="margin-bottom: 60rpx;">
		<view class="top-model">
			<customNav class="nav-compoent"></customNav>
			<!-- <u-navbar title="首页" :autoBack="true">
				<slot name="left"></slot>
			</u-navbar> -->

			<view class="search-model">
				<text class="city-text" @click="goPage('/pagesA/cityselect/cityselect')">{{city}}</text>
				<view class="search-input" style="border-left: 1px solid #fff;padding-left: 5rpx;">
					<input style="width: 100%;color: #fff;" placeholder-style="color:#fff;" type="text"
						:placeholder="$t('PleaseInputAreaDoSearch')">
				</view>
			</view>

		</view>
		<scroll-view style="padding-bottom: 100rpx;">
			<swiper class="swiper" circular indicator-color="#fff" indicator-active-color="#000" :indicator-dots="true"
				:autoplay="true" :interval="3000" :duration="2000">
				<swiper-item>
					<image class="swiper-image"
						src="http://gips0.baidu.com/it/u=1690853528,2506870245&fm=3028&app=3028&f=JPEG&fmt=auto?w=1024&h=1024"
						mode=""></image>
				</swiper-item>
				<swiper-item>
					<image class="swiper-image"
						src="http://gips3.baidu.com/it/u=100751361,1567855012&fm=3028&app=3028&f=JPEG&fmt=auto?w=960&h=1280"
						mode=""></image>
				</swiper-item>
				<swiper-item>
					<image class="swiper-image"
						src="http://gips3.baidu.com/it/u=1821127123,1149655687&fm=3028&app=3028&f=JPEG&fmt=auto?w=720&h=1280"
						mode=""></image>
				</swiper-item>
			</swiper>
			<view class="btns">
				<view class="btn-item" v-for="item in btnList" @click="goPage(item.pagePath)" :key="item.pagePath">
					<image class="img" :src="item.iconPath" mode=""></image>
					<text>{{$t(item.name)}}</text>
				</view>
			</view>
			<scroll-view scroll-x="true" class="person-list">
				<!-- <view class="person-list"> -->
					<view class="person-item" v-for="(item,index) in personList" :key="item.userId">
						<image class="person-avator" :src="item.avator" mode=""></image>
						<text class="person-name">{{item.userName}}</text>
						<view class="call-btn" @click="goPage(`/pagesB/chat/chat?userId=${item.userId}&userName=${item.userName}`, true)">{{$t('call')}}</view>
					</view>
				<!-- </view> -->
			</scroll-view>
			<view class="model-title">
				<text class="title-text">热门楼盘</text>
				<text class="more">更多</text>
			</view>
			<view class="house-list">
				<view class="house-item" @click="goDetail(item._id)" v-for="item in houseList" :key="item._id">
					<image class="house-img" :src="item.imgUrl" mode=""></image>
					<view class="house-info">
						<view class="title">{{item.title}}</view>
						<view class="description">{{item.areaNum}}m² {{item.localName}} {{item.houseType}}</view>
						<view class="price"> {{item.unitPrice}}元/m²</view>
						<view class="localtype">{{item.localType}}</view>
					</view>
				</view>
			</view>
			<up-loadmore :status="status" />
		</scroll-view>
	</view>
	<customTabbar></customTabbar>
</template>

<script setup>
	import customNav from '@/components/custom-nav/custom-nav.vue';
	import customTabbar from '@/components/custom-tabbar/custom-tabbar.vue';
	import {
		onLoad,
		onPullDownRefresh,
		onReachBottom
	} from '@dcloudio/uni-app'
	import {
		computed,
		reactive,
		ref
	} from 'vue';
	const personList = ref([{
			userId: 'zxy',
			userName: '张学友',
			avator: 'https://img0.baidu.com/it/u=4101413652,36491541&fm=253&fmt=auto&app=120&f=JPEG?w=800&h=800'
		},
		{
			userId: 'gfc',
			userName: '郭富城',
			avator: 'https://img1.baidu.com/it/u=3776569516,3711854323&fm=253&fmt=auto&app=138&f=JPEG?w=400&h=400'
		},
		{
			userId: 'zy',
			userName: '朱茵',
			avator: 'https://wx1.sinaimg.cn/mw690/c4877746ly1hbgrfhqw2wj20sp0spwjg.jpg'
		},
		{
			userId: 'zy',
			userName: '朱茵',
			avator: 'https://wx1.sinaimg.cn/mw690/c4877746ly1hbgrfhqw2wj20sp0spwjg.jpg'
		},
		{
			userId: 'zy',
			userName: '朱茵',
			avator: 'https://wx1.sinaimg.cn/mw690/c4877746ly1hbgrfhqw2wj20sp0spwjg.jpg'
		}
	])
	const city = ref('北京')
	const btnList = ref([{
			name: 'findHouse',
			// name:'找房',
			pagePath: '/pagesB/housesearch/housesearch',
			iconPath: '/static/findhouse.png'
		},
		{
			name: 'mapFindHouse',
			// name:'找房',
			pagePath: '/pagesB/map/map',
			iconPath: '/static/mapfindhouse.png'
		},
		{
			name: 'findHouse',
			// name:'找房',
			pagePath: '/pagesB/housesearch/housesearch',
			iconPath: '/static/publish.png'
		},
		{
			name: 'findHouse',
			// name:'找房',
			pagePath: '/pagesB/housesearch/housesearch',
			iconPath: '/static/publish.png'
		},
		{
			name: 'findHouse',
			// name:'找房',
			pagePath: '/pagesB/housesearch/housesearch',
			iconPath: '/static/publish.png'
		},
		{
			name: 'findHouse',
			// name:'找房',
			pagePath: '/pagesB/housesearch/housesearch',
			iconPath: '/static/publish.png'
		}
	])
	onLoad(() => {
		uni.$on('changeCity', (selectCity) => {
			console.log('我要接受' + city + '了-----')
			if (selectCity) {
				city.value = selectCity
			}
		})
	})
	const goPage = (path, flag) => {
		if(!flag) {
			uni.navigateTo({
				url: path
			})
		} else {
			const token = uni.getStorageSync('token')
			if(!token) {
				uni.navigateTo({
					url:'/pages/Login/Login'
				})
			} else {
				uni.navigateTo({
					url: path
				})
			}
		}
		
	}
	const doLocation = () => {
		uni.getLocation({
			success: function(res) {
				console.log(res)
				console.log('当前位置的经度：' + res.longitude);
				console.log('当前位置的纬度：' + res.latitude);
			},
			fail: function(err) {
				console.log(err)
			}
		})
	}
	doLocation()
	const pageInfo = reactive({
		pageSize: 6,
		pageNum: 1,
	})
	const houseList = ref([])
	const isHasMore = ref(true)
	const status = ref('loading')
	// const status = computed(() => isHasMore.value ? '加载中...' : '没有更多了')
	const getHouse = async () => {
		if (!isHasMore.value) {
			return
		}
		const res = await uniCloud.callFunction({
			name: 'list',
			data: {
				pageNum: pageInfo.pageNum,
				pageSize: pageInfo.pageSize
			}
		})
		const {
			data,
			hasMore
		} = res.result
		uni.stopPullDownRefresh()
		isHasMore.value = hasMore
		if (!isHasMore.value) {
			status.value = 'nomore'
		}
		houseList.value = [...houseList.value, ...data]
		console.log(data)
	}
	getHouse()
	const goDetail = (id) => {
		uni.navigateTo({
			url: `/pagesB/detail/detail?id=${id}`
		})
	}
	onPullDownRefresh(() => {
		console.log('下拉刷新出发嘞')
		pageInfo.pageNum = 1
		isHasMore.value = true
		houseList.value = []
		getHouse()
	})
	onReachBottom(() => {
		pageInfo.pageNum += 1
		getHouse()
	})
</script>

<style lang="scss" scoped>
	.content {
		height: 100vh;
	}

	.person-list {
		white-space: nowrap;
		margin: 20rpx 0;
		.person-item {
			display: inline-flex;
			width: 150rpx;
			// flex: 1 0 150rpx;
			margin-right: 20rpx;
			padding: 15rpx 0;
			flex-direction: column;
			align-items: center;
			justify-content: space-between;
			border: 1px solid #ccc;
			&:first-child{
				margin-left: 20rpx;
			}
			.person-avator {
				width: 80rpx;
				height: 80rpx;
				border-radius: 50%;
			}

			.person-name {
				margin: 10rpx 0;
				font-size: 26rpx;
			}

			.call-btn {
				font-size: 20rpx;
				height: 40rpx;
				line-height: 40rpx;
				text-align: center;
				width: 80rpx;
				background: linear-gradient(to right, #F96577, #FCA660);
				color: #fff;
				border-radius: 20rpx;
			}
		}
	}

	.house-list {
		padding: 0 20rpx;

		.house-item {
			display: flex;
			height: 150rpx;
			width: 100%;
			margin: 20rpx 0;

			.house-img {
				width: 150rpx;
				height: 150rpx;
				margin-right: 20rpx;
			}

			.house-info {
				flex: 1;
				display: flex;
				flex-direction: column;
				justify-content: space-evenly;

				.title {
					font-weight: bold;
				}

				.description {
					font-size: 28rpx;
					color: #aaa;
				}

				.price {
					font-size: 26rpx;
				}

				.localtype {
					display: inline;
					background: #ccc;
					font-size: 24rpx;
					border-radius: 8rpx;
					text-align: center;
					width: 80rpx;
					color: #fff;
				}
			}
		}
	}

	.top-model {
		background: linear-gradient(135deg, rgb(2, 114, 240) 0%, rgb(2, 114, 240) 0%, rgb(57, 165, 255) 100%, rgb(57, 165, 255) 100%);
		;
		padding: 20rpx;
	}

	.search-model {
		display: flex;
		background: rgba(255, 255, 255, .3);
		border-radius: 4rpx;
		padding: 10rpx;

		.search-input {
			flex: 1;
		}

		.city-text {
			padding: 0 10rpx;
			color: #fff;
		}
	}

	.swiper-image {
		width: 100%;
	}

	.btns {
		display: flex;
		flex-wrap: wrap;
		margin-top: 20rpx;

		.btn-item {
			padding-bottom: 20rpx;
			width: 20%;
			display: flex;
			flex-shrink: 0;
			flex-direction: column;
			align-items: center;

			.img {
				width: 50rpx;
				height: 50rpx;
			}
		}
	}

	.model-title {
		padding: 0 20rpx;
		display: flex;
		justify-content: space-between;
		align-items: center;

		.title-text {
			font-weight: bold;
		}

		.more {
			color: #ccc;
		}
	}
</style>